/**
 * @author: 王志昂
 * @create: 2014年6月5日
 * @note: 前端JS公共模块
 * @history:
 * 2014年6月5日 11:39 创建
 */
define(function (require, exports, module) {
    require('jquery');
    require('underscore');
    require("pop")($);
    require("select2")($);
    var ld = require('lib/jquery.loading');
    var Loading = new ld('#loadingBox', '#mask');
    var SuperbUtil = require('SuperbUtil');

    var gitTpl = require('/Public/frontapp/template/code/git_list.html');
    var subscribeGitProjectTpl = require('/Public/frontapp/template/code/subscribe_git_project_list.html');

    var CodeUtil = {
        peopleOptionTpl: '<% for (var i in peopleList) { var people = peopleList[i]; if ((typeof occupation != "undefined" && !_.contains(occupation, people.role)) || people.realname == "admin") {continue;} %><option value="<%= people.id %>" <%= _.contains(people_id, people.id) ? "selected" : "" %>><%= (people.nickname ? people.nickname : people.realname) + "--" + people.name %></option><% }; %>',
        gitSelectTpl: '<option value="0">请选择</option><% for (var i in list) { var gitProject = list[i]; %><option value="<%= gitProject.id %>" <%= _.contains(selectedGitProject, gitProject.id) ? "selected" : "" %>><%= gitProject.project_name %></option><% }; %>',
        refreshList: function () {
            window.location.reload();
        },
        // git项目管理开始
        bindAddGitButtonEvent: function () {
            $('#addGitBtn').click(function () {
                $('#add_git_window').popup({
                    filter: '#mask',
                    zindex: 9990
                });
            });
        },
        bindAddGitWindowCloseEvent: function () {
            var self = this;
            $('#add_git_window').find('.remove').click(function () {
                $('#add_git_window').pophide({
                    filter: '#mask'
                });
            });
            $('#add_git_window_cancel_btn').click(function () {
                $('#add_git_window').pophide({
                    filter: '#mask'
                });
            });
        },
        bindSaveGitButtonEvent: function (callback) {
            var self = this;
            $('#add_git_window_save_btn').click(function () {
                var id = $('#add_git_window_id').val();
                var projectName = $('#add_git_window_project_name').val();
                var gitUrl = $('#add_git_window_git_url').val();
                $.ajax({
                    url: domain + '/home/code/save_git_project',
                    method: 'GET',
                    dataType: 'json',
                    data: {
                        id: id,
                        projectName: projectName,
                        gitUrl: gitUrl
                    },
                    success: function (data) {
                        alert(data.errorMsg);
                        if (data.errorCode == 0) {
                            $('#add_git_window').find('.remove').click();
                            callback();
                        }
                    }
                });
            });
        },
        getGitList: function (searchCondition, callback) {
            var id = searchCondition.id || 0;
            var page = searchCondition.page || null;
            var pageSize = searchCondition.pageSize || null; // 不指定pageSize将无穷大
            var projectName = searchCondition.projectName || null;
            var gitUrl = searchCondition.gitUrl || null;
            $.ajax({
                url: domain + '/home/code/get_git_project_list',
                method: 'GET',
                data: {
                    id: id,
                    projectName: projectName,
                    gitUrl: gitUrl,
                    page: page,
                    pageSize: pageSize
                },
                dataType: 'json',
                success: function (data) {
                    callback(data);
                }
            });
        },
        initGitList: function (gitListCondition) {
            var self = this;
            var page, projectName, gitUrl;
            if (gitListCondition) {
                projectName = gitListCondition.projectName || '';
                gitUrl = gitListCondition.gitUrl || '';
                page = gitListCondition.page || 1;
            } else {
                projectName = '';
                gitUrl = '';
                page = 1;
            }

            Loading.show();
            var searchGitCondition = {
                id: 0,
                page: page,
                pageSize: 15,
                projectName: projectName,
                gitUrl: gitUrl
            };
            self.getGitList(searchGitCondition, function (data) {
                var gitListHtml = _.template(gitTpl, {
                    list: data.data.content
                });
                $("#gitContent").html(gitListHtml);
                SuperbUtil.moreData(data, page, searchGitCondition, self, null);
                Loading.hide();
            });
        },
        bindPullCodeButtonEvent: function () {
            $('#pullCode').click(function () {
                if (!confirm('你确定要把代码拉到superb的服务器上吗？可以用作任务选择和接口自动生成哦。')) {
                    return false;
                }
                //$.ajax({
                //    url : ,
                //
                //});
            });
        },
        // git项目管理结束

        // 订阅代码开始
        bindAddSubscribeButtonEvent: function (callback) {
            $('#addSubscribeBtn').click(function () {
                $('#add_subscribe_window').popup({
                    filter: '#mask',
                    zindex: 9990
                });
                if (typeof callback == 'function') {
                    callback();
                }
            });
        },
        bindAddSubscribeWindowCloseEvent: function () {
            var self = this;
            $('#add_subscribe_window').find('.remove').click(function () {
                $('#add_subscribe_window').pophide({
                    filter: '#mask'
                });
            });
            $('#add_subscribe_window_cancel_btn').click(function () {
                $('#add_subscribe_window').pophide({
                    filter: '#mask'
                });
            });
        },
        bindEditSubscribeButtonEvent: function () {
            var self = this;
            $(document).on('click', '.editBtn', function () {
                var userIds = $(this).siblings('.userIds').val();
                var userIdArr = userIds.split(',');
                var projectName = $(this).parent().siblings('.project_name').text();
                var gitProjectSelector = $("#add_subscribe_window_git_project_id");
                gitProjectSelector.find('option').each(function () {
                    if ($(this).text() == projectName) {
                        $(this).attr("selected", true);
                    }
                });
                gitProjectSelector.select2();

                var userSelector = $('#add_subscribe_window_user_id');
                userSelector.find('option').each(function () {
                    if ($.inArray($(this).val(), userIdArr) >= 0) {
                        $(this).attr("selected", true);
                    }
                });
                userSelector.select2();

                $('#add_subscribe_window').popup({
                    filter: '#mask',
                    zindex: 9990
                });
            });
        },
        renderAddSubscribeWindowPeopleSelect: function (peopleList, selectedUsers) {
            selectedUsers = selectedUsers || "";
            var usersArr = selectedUsers.split(",");
            var usersOption = _.template(this.peopleOptionTpl, {
                peopleList: peopleList,
                people_id: usersArr,
                _: _
            });
            $('#add_subscribe_window_user_id').html(usersOption);
            $('#add_subscribe_window_user_id').select2();
        },
        renderAddSubscribeWindowGitProjectList: function () {
            var self = this;
            var searchGitCondition = {
                id: 0,
                page: 1,
                projectName: '',
                gitUrl: ''
            };
            self.getGitList(searchGitCondition, function (data) {
                var gitSelectListHtml = _.template(self.gitSelectTpl, {
                    list: data.data.content,
                    selectedGitProject: []
                });
                $("#add_subscribe_window_git_project_id").html(gitSelectListHtml).select2();
            });
        },
        getSubscribeGitProjectList: function (searchCondition, callback) {
            var page = searchCondition.page || null;
            var pageSize = searchCondition.pageSize || null; // 不指定pageSize将无穷大
            var projectName = searchCondition.projectName || null;
            $.ajax({
                url: domain + '/home/code/get_subscribe_git_project_list',
                method: 'GET',
                data: {
                    projectName: projectName,
                    page: page,
                    pageSize: pageSize
                },
                dataType: 'json',
                success: function (data) {
                    callback(data);
                }
            });
        },
        initSubscribeGitProjectList: function (subscribeGitListCondition) {
            var self = this;
            var page = 1, projectName;
            if (subscribeGitListCondition) {
                projectName = subscribeGitListCondition.projectName || '';
            } else {
                projectName = '';
            }

            Loading.show();
            var searchGitCondition = {
                page: page,
                pageSize: 15,
                projectName: projectName
            };
            self.getSubscribeGitProjectList(searchGitCondition, function (data) {
                var gitListHtml = _.template(subscribeGitProjectTpl, {
                    list: data.data.content
                });
                $("#subscribeGitProjectContent").html(gitListHtml);
                SuperbUtil.moreData(data, page, searchGitCondition, self, null);
                Loading.hide();
            });
        },
        bindSaveSubscribeGitProjectButtonEvent: function (callback) {
            var self = this;
            $('#add_subscribe_window_save_btn').click(function () {
                var projectId = $('#add_subscribe_window_git_project_id').val();
                var userIds = $('#add_subscribe_window_user_id').val();
                $.ajax({
                    url: domain + '/home/code/save_subscribe_git_project',
                    method: 'GET',
                    dataType: 'json',
                    data: {
                        projectId: projectId,
                        userIds: userIds
                    },
                    success: function (data) {
                        alert(data.errorMsg);
                        if (data.errorCode == 0) {
                            $('#add_subscribe_window').find('.remove').click();
                            callback();
                        }
                    }
                });
            });
        },
        bindPageEvent: function (searchGitCondition, page) {
            var self = this;
            searchGitCondition.page = page;
            self.initGitList(searchGitCondition);
        }
        // 订阅代码结束
    };
    module.exports = CodeUtil;
});
